package com.ncu.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MainReceiver extends BroadcastReceiver {

    public static final String RECEIVER_ACTION = "com.android.server.scannerservice.broadcastbl";

    private DeviceManagement deviceManagement;

    private  BarCodeReceiver receiver;

    public void setDeviceManagement(DeviceManagement deviceManagement){
        this.deviceManagement =  deviceManagement;
    }

    public void setReceiver(BarCodeReceiver receiver){
        this.receiver = receiver;

    }
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.e("MainActivity", "---------------------接收到广播-------------");
        String action = intent.getAction();
        if (RECEIVER_ACTION.equals(action)) {
            String data = intent.getStringExtra("scannerdata");
            Log.e("MainActivity", "-------------------data=" + data);
            if(deviceManagement!=null){
                BarCodeData.Stub barCodeData = new BarCodeData.Stub() {
                    @Override
                    public void GetBarCodeData(String msg, String data) throws RemoteException {
                        Log.e("MainActivity", "扫描状态：" + msg + "\t扫描数据：" + data);
                        intent.putExtra("scannerdata",data);
                        context.sendBroadcast(intent);
                    }
                };
                deviceManagement.startBarcodeScan(barCodeData);
            }
        }
    }
}
